<template>
	<view>
		<!-- 状态栏 -->
		<page-status ></page-status>
		<!-- 导航条 -->
		<page-head></page-head>
		<!-- 轮播图 -->
		<view class="swiper">
			<view class="swiper-box">
				<swiper :autoplay="true" :circular="true" @change="swiperChange">
					<swiper-item v-for="swiper in swiperList" :key="swiper.id">
						<image :src="swiper.img" ></image>
					</swiper-item>
				</swiper>
				<view class="indicator">
					<view
						class="dots"
						v-for="(swiper, index) in swiperList"
						:class="{'on': currentSwiper >= index}"
						:key="index"
					></view>
				</view>
			</view>
		</view>
		<!-- 分类列表 -->
		<view class="category-list">
			<view 
				class="category" 
				v-for="(row,index) in categoryList" 
				:key="index"
				@tap="toCategory(row)"
			>
				<view class="img">
					<image :src="row.img"></image>
				</view>
				<view class="text">
					{{row.name}}
				</view>
			</view>
		</view>
		<!--  广告图 -->
		<view class="banner" v-if="promotion.length > 0">
			<image src="/static/img/category/ad.jpg"></image>
		</view>
		<!-- 活动区 -->
		<view class="promotion" v-if="promotion.length > 0">
			<view class="text">优惠活动</view>
			<view class="list">
				<view 
					class="column"
					v-for="(row,index) in promotion"
					:key="index"
					@tap="toPromotion(row)"
				>
					<view class="top">
						<view class="title">{{ row.title }}</view>
					</view>
					<view class="left">
						<view class="ad">{{ row.ad }}</view>
						<view class="into">点击进入</view>
					</view>
					<view class="right">
						<image :src="row.img"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import pageHead from './pageHeader.vue'
	import interfaces from '../../../utils/interfaces.js'

	export default {
		data() {
			return {
				// 轮播图片
				swiperList: [],
				// 分类菜单
				categoryList: [],
				promotion: [],
				// 当前轮播图显示下标
				currentSwiper: 0
			}
		},
		components: {
			pageHead
		},
		onLoad() {
			this.initData()
		},
		methods: {
			initData() {
				this.request({
					url: interfaces.getMallData,
					success: res => {
						this.swiperList = res.data.swiperList
						this.categoryList = res.data.categoryList
						this.promotion = res.data.promotion
					}
				})
			},
			//轮播图指示器
			swiperChange(event) {
				// console.log(event.detail.current)
				this.currentSwiper = event.detail.current
			},
			//分类跳转，e为形参，row为实参，包括图片img，图片name
			toCategory(e) {
				// console.log(e.name)
				uni.navigateTo({
					url: '../../goods/goodsList?name=' + e.name
				})
			},
			//推荐商品跳转
			toPromotion(e) {
				uni.showToast({ title: e.title, icon: 'none' })
			},
		}
		
	}
</script>

<style lang="scss">
	.swiper {
		width: 100%;
		margin-top: 10upx;
		display: flex;
		justify-content: center;
		.swiper-box {
			width: 92%;
			height: 30.7vw;
			overflow: hidden;
			border-radius: 15upx;
			box-shadow: 0upx 8upx 25upx rgba(0, 0, 0, 0.2);
			//兼容ios，微信小程序
			position: relative;
			z-index: 1;
			swiper {
				width: 100%;
				height: 30.7vw;
				swiper-item {
					image {
						width: 100%;
						height: 40vw;
					}
				}
			}
			.indicator {
				position: absolute;
				bottom: 20upx;
				left: 20upx;
				background-color: rgba(255, 255, 255, 0.4);
				width: 150upx;
				height: 5upx;
				border-radius: 3upx;
				overflow: hidden;
				display: flex;
				.dots {
					width: 0upx;
					background-color: rgba(255, 255, 255, 1);
					transition: all 0.3s ease-out;
					&.on {
						width: (100%/3);
					}
				}
			}
		}
	}
	.category-list {
		width: 92%;
		margin: 0 4%;
		padding: 0 0 30rpx 0;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;	
		border-bottom: solid 2rpx #f6f6f6;
		
		.category {
			width: 25%;
			margin-top: 50rpx;
			display: flex;
			flex-wrap: wrap;
			
			.img {
				width: 100%;
				display: flex;
				justify-content: center;
				
				image {
					width: 11vw;
					height: 11vw;
				}
			}
			
			.text {
				margin-top: 16rpx;
				width: 100%;
				display: flex;
				justify-content: center;
				font-size: 24rpx;
				color: #3c3c3c;
			}
		}
	}
	.banner {
		width: 92%;
		margin: 40rpx 4%;
		
		image {
			width: 100%;
			height: 36vw;
			border-radius: 4rpx;
			box-shadow: 0rpx 5rpx 25rpx rgba(0, 0, 0, 0.3);
		}
	}
	.promotion {
		width: 92%;
		margin: 10rpx 4% 30rpx 4%;
		
		.text {
			width: 100%;
			height: 60rpx;
			font-size: 34rpx;
			font-weight: 600;
			margin: 10rpx;
		}
		
		.list {
			width: 100%;
			display: flex;
			justify-content: space-between;
			
			.column {
				width: 43%;
				padding: 15rpx 3%;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				border-radius: 10rpx;
				background-color: #ebf9f9;
				overflow: hidden;
				
				.top {
					width: 100%;
					height: 40rpx;
					margin-bottom: 5rpx;
					display: flex;
					align-items: center;
					
					.title {
						font-size: 30rpx;
					}
				}
				
				.left {
					width: 50%;
					height: 18.86vw;
					display: flex;
					align-content: space-between;
					flex-wrap: wrap;
					
					.ad {
						width: 100%;
						margin-top: 5rpx;
						font-size: 22rpx;
						color: #acb0b0;
					}
					
					.into {
						width: 100%;
						margin-bottom: 5rpx;
						font-size: 24rpx;
						color: #aaa;
					}
				}
				
				.right {
					width: 18.86vw;
					height: 18.86vw;
					
					image {
						width: 18.86vw;
						height: 18.86vw;
					}
				}
			}
		}
	}
</style>
